home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MACSHELL / MS1 / COMMANDS / DIRCMDS.C < prev    next >
Text File  |  1992-12-02  |  7KB  |  280 lines

  1. /*
  2.  *    MacShell Source File
  3.  *
  4.  *    Copyright (c) 1989, 1990, 1991, 1992  Suick Bay Technologies.  All rights reserved.
  5.  *
  6.  *
  7.  *    RESTRICTIONS ON MacShell program and source code.
  8.  *
  9.  *    Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
  10.  *    restricted use by the owner of the CDROM "Disk to the future II".
  11.  *
  12.  *    Ñ╩No permission is granted for any commercial use without the written
  13.  *    consent of the Suick Bay Technologies.
  14.  *
  15.  *    Ñ╩No permission is granted for any redistribution of any kind use without
  16.  *    the written consent of the Suick Bay Technologies.
  17.  *
  18.  *    Ñ╩Permission is granted to use this for any personal noncommercial use.
  19.  *
  20.  *    Ñ╩You may not distribute source or executable code at all, nor may you 
  21.  *    distribute it with or within a commercial product without the written
  22.  *    consent of the Suick Bay Technologies.  Please send modifications to 
  23.  *    the author for inclusion in updates to the program.  Thanks.
  24.  *
  25.  *
  26.  *    MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  27.  *    WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  28.  *    PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  29.  *
  30.  *    SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  31.  *    INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
  32.  *    OR ANY PART THEREOF. 
  33.  *
  34.  *    In no event will Suick Bay Technologies be liable for any lost revenue
  35.  *    or profits or other special, indirect and consequential damages, even if
  36.  *    Suick Bay Technologies has been advised of the possibility of such damages.
  37.  *
  38.  *    Suick Bay Technologies can be reached at:
  39.  *    
  40.  *    8768 Cottonwood lane
  41.  *    Maple Grove, MN 55369
  42.  *    Voice: (612) 425-7025
  43.  *    AppleLink: D5233
  44.  *    
  45.  *
  46.  *    No parts of this software may be reproduced or stored in a
  47.  *    retrieval system or transmitted in any form, or any means,
  48.  *    electronic, mechanical, photocopying, recording or otherwise,
  49.  *    without the prior written permission of Suick Bay Technologies.
  50.  *    
  51.  *    Spread the word and not the disk.
  52.  *    
  53.  *    SPK 030190    :    Fixed bug in rmdir subdir name (dir--)
  54.  *    SPK 012290    :    Initial
  55.  */
  56.  
  57. #include    "SystemPub.h"
  58. #include    "Proc.h"
  59. #include    "Path.h"
  60. #include    "ShellPub.h"
  61.  
  62. /********************************************************************/
  63.  
  64. void        MKDIRdirs( WHandle ShellWh, int16 ProcID, char *argument )
  65. {
  66. ShellWindRec    **MyShell;
  67. int16                err, vRefNum;
  68. pathType        pt;
  69. char            dirPath[ 256 ], *dir;
  70. int32            dirID, newDirID;
  71.  
  72.     MyShell = (ShellWindRec **) (**ShellWh).thing;
  73.     strcpy( dirPath, argument );
  74.     
  75.     dir = ExtractFile( dirPath );    /* actually gets last name */
  76.     
  77.     if( dir == dirPath )        /* no path, argument is directory name */
  78.         {
  79.         CtoPstr( dir );
  80.         err = DirCreate( (**MyShell).pwdVRefNum, (**MyShell).pwdDirID,
  81.                  dir, &newDirID );
  82.         if( err )
  83.             FileError ( err );
  84.         }
  85.     else                /* path to the directory name */
  86.         {
  87.         dir--;
  88.         *dir++ = '\0';    /* now points to the dir name */
  89.         
  90.         pt = SetCurrPath( dirPath );
  91.         
  92.         if( pt == pathIsDir )
  93.             {
  94.             err = HGetVol( NULL, &vRefNum, &dirID );
  95.             CtoPstr( dir );
  96.             err = DirCreate( vRefNum, dirID, dir, &newDirID );
  97.             if( err )
  98.                 FileError ( err );
  99.             }
  100.         }
  101.  
  102.     ResetShellPWD( ShellWh );
  103. }
  104.  
  105. /*******************************************************************/
  106.  
  107. Boolean            DoMKDIR( int16 ProcToken, WHandle ShellWh, int16 ProcID,
  108.                     char *string )
  109. {
  110. int16                i, argc;
  111. char            *cp, argument[ 256 ];
  112. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  113.  
  114.     switch( ProcToken )
  115.         {
  116.         case    PROC_INIT    :
  117.             (**MyShell).Proc[ ProcID ].flags = TRUE;
  118.             break;
  119.             
  120.         case    PROC_TERM    :
  121.         case    PROC_BREAK    :
  122.             /* Tell the shell that we're done */
  123.             SendOutToken( ShellWh, ProcID, PROC_BREAK );
  124.             /* Turn ourself off */
  125.             (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  126.             break;
  127.             
  128.         case    PROC_STDIN    :
  129.             if( (**MyShell).Proc[ ProcID ].flags )
  130.                 {
  131.                 (**MyShell).Proc[ ProcID ].flags = FALSE;        
  132.  
  133.                 for( i = 1; i < (**MyShell).Proc[ ProcID ].argc; i++ )
  134.                     {
  135.                     GetArgv( ShellWh, ProcID, i, argument );
  136.                     MKDIRdirs( ShellWh, ProcID, argument );
  137.                     }
  138.  
  139.                 /* Tell the shell that we're done */
  140.                 SendOutToken(  ShellWh, ProcID, PROC_BREAK );
  141.                 
  142.                 /* Turn ourself off */
  143.                 (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  144.                 return( FALSE );
  145.                 }
  146.         }
  147. }
  148.  
  149.  
  150. /*******************************************************************/
  151.  
  152. Boolean        rmfmode;
  153.  
  154. Boolean     DirIsEmpty( char *name, int16 vRefNum, int32 dirID )
  155. {
  156. CInfoPBRec    scanPB;
  157. char        scan[ 256 ];
  158. OSErr        err;
  159.  
  160.     strcpy( scan, name );
  161.     CtoPstr( scan );
  162.  
  163.     scanPB.dirInfo.ioFDirIndex     = 0;        /* name in this directory */
  164.     scanPB.dirInfo.ioCompletion = 0L; 
  165.     scanPB.dirInfo.ioNamePtr    = (StringPtr) scan;
  166.     scanPB.dirInfo.ioVRefNum     = vRefNum;
  167.     scanPB.dirInfo.ioDrDirID    = dirID;    /* parent specified */
  168.     
  169.     err = PBGetCatInfo( &scanPB, FALSE );
  170.     if( err )
  171.         return( FALSE );
  172.     
  173.     return( scanPB.dirInfo.ioDrNmFls == 0 );
  174. }
  175.  
  176.  
  177. void        RMDIRCallBack( WHandle ShellWh, int16 ProcID,
  178.                 char *path, char *last,
  179.                 pathType what, int16 vRefNum, int32 dirID )
  180. {
  181. OSErr    err;
  182.  
  183.     if( what == pathIsDir )
  184.         {
  185.         if( rmfmode )    /* force the delete of the directory */
  186.             {
  187.             err = RmTree( last, vRefNum, dirID );
  188.             if( err )
  189.                 FileError ( err );
  190.             }
  191.         else if( DirIsEmpty( last, vRefNum, dirID ) )
  192.             {
  193.             CtoPstr( last );
  194.             err = HDelete( vRefNum, dirID, last );
  195.             if( err )
  196.                 FileError ( err );
  197.             }
  198.         else
  199.             procPrintf( ShellWh, ProcID,
  200.                 "rmdir : %s is not empty, use -f to force removal.\n", last );
  201.         }
  202.     else
  203.         procPrintf( ShellWh, ProcID, "rmdir : can't remove %s.\n", last );
  204. }
  205.  
  206. void        RMDIRdirs( WHandle ShellWh, int16 ProcID, char *argument )
  207. {
  208. ShellWindRec    **MyShell;
  209.  
  210.     MyShell = (ShellWindRec **) (**ShellWh).thing;
  211.  
  212.     ExpandPath( ShellWh, ProcID, argument, (ProcPtr) RMDIRCallBack,
  213.             (**MyShell).pwdVRefNum, (**MyShell).pwdDirID );
  214.  
  215.     ResetShellPWD( ShellWh );
  216. }
  217.  
  218. /*******************************************************************/
  219.  
  220. Boolean            DoRMDIR( int16 ProcToken, WHandle ShellWh, int16 ProcID,
  221.                      char *string )
  222. {
  223. int16                i, argc;
  224. char            *cp, argument[ 256 ];
  225. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  226.  
  227.     switch( ProcToken )
  228.         {
  229.         case    PROC_INIT    :
  230.             (**MyShell).Proc[ ProcID ].flags = TRUE;
  231.             break;
  232.             
  233.         case    PROC_TERM    :
  234.         case    PROC_BREAK    :
  235.             /* Tell the shell that we're done */
  236.             SendOutToken( ShellWh, ProcID, PROC_BREAK );
  237.             /* Turn ourself off */
  238.             (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  239.             break;
  240.             
  241.         case    PROC_STDIN    :
  242.             if( (**MyShell).Proc[ ProcID ].flags )
  243.                 {
  244.                 (**MyShell).Proc[ ProcID ].flags = FALSE;        
  245.                 rmfmode = FALSE;
  246.                 
  247.                 /* get arguments */
  248.                 argc = (**MyShell).Proc[ ProcID ].argc;
  249.                 for( i = 1; i < argc; i++ )
  250.                     {
  251.                     GetArgv( ShellWh, ProcID, i, argument );
  252.                     cp = argument;
  253.         
  254.                     if( *cp++ == '-' )
  255.                         while( *cp )
  256.                             switch( *cp++ )
  257.                                 {
  258.                                 case    'f'    :    /* option x */
  259.                                     rmfmode = TRUE;
  260.                                     break;
  261.                                 }
  262.                     }
  263.                 for( i = 1; i < argc; i++ )
  264.                     {
  265.                     GetArgv( ShellWh, ProcID, i, argument );
  266.                     if( *argument != '-' )
  267.                         RMDIRdirs( ShellWh, ProcID, argument );
  268.                     }
  269.  
  270.                 /* Tell the shell that we're done */
  271.                 SendOutToken(  ShellWh, ProcID, PROC_BREAK );
  272.                 
  273.                 /* Turn ourself off */
  274.                 (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  275.                 return( FALSE );
  276.                 }
  277.             return( FALSE );
  278.         }
  279. }
  280.